'use strict';

/* Services */

angular.module('placeServices', []).
    factory('Places', function($q, $rootScope) {
    	return {
	    	search: function (map, query) {
	    		var service = new google.maps.places.PlacesService(map),
					places = $q.defer(),
					request = {
		    		 	query:query,
						types: ['restaurant'],
						bounds:map.getBounds()
					};
	    		service.textSearch(request, function(result, status) {
	    			if (status == google.maps.places.PlacesServiceStatus.OK) {
		    			$rootScope.$apply(function() {
			    			places.resolve(result);
		    			});
		    		}
	    		});
	    		return places.promise;
	    	},
	    	get: function (map, reference) {
	    		var service = new google.maps.places.PlacesService(map),
	    			place = $q.defer(),
	    			request = {
				 		reference:reference
					};
				
				service.getDetails(request, function callback(result, status) {
					if (status == google.maps.places.PlacesServiceStatus.OK) {
						$rootScope.$apply(function() {
			    			place.resolve(result);
		    			});	
					}
				});
				return place.promise;
	    	}
    	};
});
